package cn.kgc.vue.controller;

import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/4/15
 * @description 权限模块
 */
@RestController
@RequestMapping("auth")
@Api(tags = "权限模块")
public class AuthorizationController {

    @Resource
    private PermissionService permissionService;

    @GetMapping("tree")
    @ApiOperation("权限Tree")
    public ResponseResult authTree() {

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.select(Permission::getId, Permission::getTitle, Permission::getPid);

        List<Permission> list = permissionService.list(lambda);
        return ResponseResult.success().data("tree", list);
    }

    @GetMapping("list")
    @ApiOperation("权限列表")
    public ResponseResult authList() {

        List<Permission> list = permissionService.list();

        return ResponseResult.success().data("list",list);
    }

    @PostMapping("addAuth")
    @ApiOperation("添加权限")
    public ResponseResult addAuth(@RequestBody Permission permission) {
        System.out.println("permission = " + permission);
        // permissionService.save(permission);

        permissionService.saveOrUpdate(permission);
        return ResponseResult.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation("根据id获取权限")
    public ResponseResult getAuthById(@PathVariable("id") Integer id) {
        Permission permission = permissionService.getById(id);
        // permission  不是目录
        Integer pid = permission.getPid();
        String title = "";
        if (pid!=0){
           title = permissionService.getById(permission.getPid()).getTitle();
        }
        return ResponseResult.success()
                .data("permission",permission)
                .data("parentTitle",title);
    }


    @DeleteMapping("delete/{id}")
    @ApiOperation("根据id删除权限")
    public ResponseResult deleteAuth(@PathVariable("id") Integer id) {

        return permissionService.delte(id);
    }

}
